package com.uroad.lib.widget.photopicker.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.uroad.lib.R;
import com.uroad.lib.imageloader.ImageLoaderHelper;
import com.uroad.lib.widget.dialog.DialogHelper;
import com.uroad.lib.widget.photopicker.PhotoPickUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;


public class MultiPickResultView extends FrameLayout {

    @IntDef({ACTION_SELECT, ACTION_ONLY_SHOW})

    //Tell the compiler not to store annotation data in the .class file
    @Retention(RetentionPolicy.SOURCE)

    //Declare the NavigationMode annotation
    public @interface MultiPicAction {
    }

    //图片质量，默认标清。考虑到兼容以前的项目，新增一个方法设置图片质量，没有设置的按默认质量（标清）
    private int quality = ImageLoaderHelper.PIC_QUALITY_RGB_565;

    public void setQuality(int quality) {
        this.quality = quality;
    }

    public static final int ACTION_SELECT = 1;//该组件用于图片选择
    public static final int ACTION_ONLY_SHOW = 2;//该组件仅用于图片显示

    public static final int SHOW_TYPE_SELF_ADAPTION = 0;//小于3张，自适应
    public static final int SHOW_TYPE_DEFAULT = 1;//默认，不做自适应处理

    private int action, size = 9, lineSize = 3, defultPic = R.drawable.ic_picker_pic_default;
    private int showType = SHOW_TYPE_SELF_ADAPTION;//显示图片的模式

    RecyclerView recyclerView;
    PhotoAdapter photoAdapter;
    ArrayList<String> selectedPhotos;

    public MultiPickResultView(Context context) {
        this(context, null, 0);
    }

    public MultiPickResultView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
        initData(context, attrs);
        initEvent(context, attrs);
    }

    protected void onRestoreInstanceState(Parcelable state) {
        try {
            super.onRestoreInstanceState(state);
        } catch (Exception e) {

        }
        state = null;
    }

    private void initEvent(Context context, AttributeSet attrs) {

    }

    private void initData(Context context, AttributeSet attrs) {

    }

    private void initView(Context context, AttributeSet attrs) {

        recyclerView = new RecyclerView(context, attrs);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(lineSize, OrientationHelper.VERTICAL));
        this.addView(recyclerView);
    }

    public void setDefultPic(int defultPic) {
        this.defultPic = defultPic;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MultiPickResultView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }

    public void init(Activity context, @MultiPicAction int action, ArrayList<String> photos, int size) {
        this.action = action;
        this.size = size;
        if (action == MultiPickResultView.ACTION_ONLY_SHOW) {//当只用作显示图片时,一行显示3张
            if (showType == SHOW_TYPE_SELF_ADAPTION) {
                if (photos != null && photos.size() < 3) {//当不超过3张，一行显示1张或2张
                    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(photos.size(), OrientationHelper.VERTICAL) {
                        @Override
                        public boolean canScrollVertically() {//禁止滚动
                            return false;
                        }
                    });

                } else {
                    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(lineSize, OrientationHelper.VERTICAL));
                }
            } else {
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(lineSize, OrientationHelper.VERTICAL));
            }

        }

        selectedPhotos = new ArrayList<>();

        this.action = action;
        if (photos != null && photos.size() > 0) {
            selectedPhotos.addAll(photos);
        }
        photoAdapter = new PhotoAdapter(context, selectedPhotos);
        photoAdapter.quality = quality;
        photoAdapter.setAction(action);
        photoAdapter.setSize(size);
        photoAdapter.setDefultPic(defultPic);
        recyclerView.setAdapter(photoAdapter);
        //recyclerView.setLayoutFrozen(true);
    }


    public void showPics(List<String> paths) {
        if (paths != null) {
            selectedPhotos.clear();
            selectedPhotos.addAll(paths);
            photoAdapter.notifyDataSetChanged();
        }

    }

    //清空已选择的图片
    public void clear() {
        selectedPhotos.clear();
        photoAdapter.notifyDataSetChanged();
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (action == ACTION_SELECT) {
            PhotoPickUtils.onActivityResult(requestCode, resultCode, data, new PhotoPickUtils.PickHandler() {
                @Override
                public void onPickSuccess(ArrayList<String> photos) {
                    photoAdapter.refresh(photos);
                }

                @Override
                public void onPreviewBack(ArrayList<String> photos) {
                    photoAdapter.refresh(photos);
                }

                @Override
                public void onPickFail(String error) {
                    DialogHelper.showShortTost(getContext(), error);
                    selectedPhotos.clear();
                    photoAdapter.notifyDataSetChanged();
                }

                @Override
                public void onPickCancle() {
                }
            });
        }

    }


    public ArrayList<String> getPhotos() {
        return selectedPhotos;
    }

    public void addPic() {
        photoAdapter.addPic();
    }

    //设置一行最多显示多少张图片
    public void setLineSize(int lineSize) {
        this.lineSize = lineSize;
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(lineSize, OrientationHelper.VERTICAL));
    }

    /**
     * 设置显示图片的模式
     *
     * @author hrd
     * @date 2017/8/30 0030
     */
    public void setShowType(int type) {
        showType = type;
    }

}
